import 'package:flutter/material.dart';

class QrcodeLineView extends StatefulWidget {
  const QrcodeLineView({super.key});

  @override
  State<QrcodeLineView> createState() => _QrcodeLineViewState();
}

class _QrcodeLineViewState extends State<QrcodeLineView>
    with TickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController controller;

  //起始之间的线性插值器 从 0.05 到 0.95 百分比。
  final Tween<double> rotationTween = Tween(begin: 0, end: 1);

  @override
  void initState() {
    super.initState();

    controller = AnimationController(
      vsync: this, //实现 TickerProviderStateMixin
      duration: const Duration(seconds: 2), //动画时间 3s
    );

    animation = rotationTween.animate(controller)
      ..addListener(() => setState(() {}))
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.repeat();
        } else if (status == AnimationStatus.dismissed) {
          controller.forward();
        }
      });

    controller.repeat();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var height = MediaQuery.of(context).size.height - 300;
    return Positioned(
      top: 100,
      left: 0,
      right: 0,
      bottom: 200,
      child: Stack(
        children: [
          Positioned(
            left: 0,
            right: 0,
            top: animation.value * height,
            child: Container(
              height: 130,
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('res/images/scan.png'),
                  fit: BoxFit.fitWidth,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
